<template>
	<div class="workflow-form-container layout-padding">
		<el-card shadow="hover" class="mb15">
			<div class="workflow-form-search">
				<el-input v-model="state.tableData.param.filter" size="default" placeholder="请输入表单名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="searchForm">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
			</div>
		</el-card>
		<el-card shadow="hover" class="layout-padding-auto">
			<template #header>
				<el-button v-auth="'FormManagement.Form.Create'" size="default" type="success" @click="onOpenAddForm('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增表单
				</el-button>
			</template>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="formName" label="表单名称" width="150" show-overflow-tooltip></el-table-column>
				<el-table-column prop="api" label="Api接口" show-overflow-tooltip></el-table-column>
				<el-table-column prop="description" label="描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="disabled" label="是否禁用" width="90" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.disabled">已禁用</el-tag>
						<el-tag type="info" v-else>未禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template #default="scope">
						<el-button v-auth="'FormManagement.Form.Update'" size="small" text type="warning" @click="onOpenEditForm('edit', scope.row)">修改</el-button>
						<el-button v-auth="'FormManagement.Form.Delete'" size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				v-model:current-page="state.tableData.currentPage"
				background
				v-model:page-size="state.tableData.param.maxResultCount"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="workflowForm">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useFormApi } from '/@/api/form/index';
import { useRouter } from 'vue-router';

// 引入表单 Api 请求接口
const formApi = useFormApi();

// 定义变量内容
const router = useRouter();
const formDialogRef = ref();
const state = reactive<WfFormState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		currentPage: 1,
		param: {
			filter: "",
			sorting: "formName",
			skipCount: 0,
			maxResultCount: 10
		}
	}
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	state.tableData.param.skipCount = (state.tableData.currentPage - 1) * state.tableData.param.maxResultCount;
	formApi.getFormList(state.tableData.param).then((result: any) => {
		state.tableData.total = result.totalCount;
		state.tableData.data = result.items;
		state.tableData.loading = false;
	}).catch(err => {
		state.tableData.loading = false;
		console.error("查询表单列表出错：", err);
	});
};

// 搜索
const searchForm = () => {
	state.tableData.currentPage = 1;
	getTableData();
}

// 打开新增表单弹窗
const onOpenAddForm = (type: string) => {
	// formDialogRef.value.openDialog(type);
	router.push({ path: "/workflow/form/create" });
};

// 打开修改表单弹窗
const onOpenEditForm = (type: string, row: RowFormType) => {
	formDialogRef.value.openDialog(type, row);
};

// 删除表单
const onRowDel = (row: RowFormType) => {
	ElMessageBox.confirm(`此操作将永久删除表单名称：“${row.formName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(() => {
		let delIds = [row.id];
		formApi.deleteForm(delIds).then(result => {
			if(result) {
				getTableData();
				ElMessage.success('删除成功');
			}
		}).catch(err => {
			console.error("提交出错：", err);
		});
	});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.maxResultCount = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.currentPage = val;
	state.tableData.param.skipCount = (state.tableData.currentPage - 1) * state.tableData.param.maxResultCount;
	getTableData();
};

// 页面加载时
onMounted(() => {
	
	getTableData();
});
</script>

<style scoped lang="scss">
.workflow-form-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
